package com.ruantu.clothing.service.impl;

import com.ruantu.clothing.dao.GongyingshangMapper;
import com.ruantu.clothing.entity.Gongyingshang;
import com.ruantu.clothing.entity.PageBean;
import com.ruantu.clothing.service.GongyingshangService;
import com.ruantu.clothing.util.MyException;
import com.ruantu.clothing.util.UUIDUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/6/7 0007.
 */
@Service
public class GongyingshangServiceImpl implements GongyingshangService {

    @Autowired
    private GongyingshangMapper gongyingshangMapper;

    @Override
    public List<Gongyingshang> findAll(String jgid) {
        return gongyingshangMapper.findAll(jgid);
    }

    @Override
    public List<Gongyingshang> findGongyingshangList(Gongyingshang gongyingshang, PageBean pageBean) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.isNotBlank(gongyingshang.getJgId())) {
            map.put("jgId", gongyingshang.getJgId());
        }
        if (StringUtils.isNotBlank(gongyingshang.getGname())) {
            map.put("gname", gongyingshang.getGname());
        }
        if (gongyingshang.getLeixing() != null && gongyingshang.getLeixing() != -1) {
            map.put("leixing", gongyingshang.getLeixing());
        }
        map.put("start", pageBean.getStart());
        map.put("rows", pageBean.getRows());
        pageBean.setTotal(gongyingshangMapper.findGongyingshangCount(map));
        return gongyingshangMapper.findGongyingshang(map);
    }

    @Override
    public XSSFWorkbook downExcel(Gongyingshang gongyingshang) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.isNotBlank(gongyingshang.getJgId())) {
            map.put("jgId", gongyingshang.getJgId());
        }
        if (StringUtils.isNotBlank(gongyingshang.getGname())) {
            map.put("gname", gongyingshang.getGname());
        }
        if (gongyingshang.getLeixing() != null && gongyingshang.getLeixing() != -1) {
            map.put("leixing", gongyingshang.getLeixing());
        }
        List<Gongyingshang> list = gongyingshangMapper.findGongyingshang(map);
        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFSheet sheet = wb.createSheet("sheet1");
        CellStyle titleStyle = wb.createCellStyle();
        titleStyle.setFillForegroundColor(HSSFColor.GREY_50_PERCENT.index);
        titleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        XSSFFont font = wb.createFont();
        font.setColor(IndexedColors.WHITE.getIndex());
        titleStyle.setFont(font);
        // 设置这些样式
        int count = 0;
        XSSFRow row = sheet.createRow(0);
        XSSFCell cell = row.createCell(0);
        cell.setCellValue("供应商名称");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(++count);
        cell.setCellValue("供应商类型");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(++count);
        cell.setCellValue("负责人");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(++count);
        cell.setCellValue("联系方式");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(++count);
        cell.setCellValue("地址");
        cell.setCellStyle(titleStyle);
        CellStyle titleStyle2 = wb.createCellStyle();
        titleStyle2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        titleStyle2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        for (int i = 0; i < list.size(); i++) {
            count = 0;
            row = sheet.createRow(i + 1);
            //供应商名称
            cell = row.createCell(count);
            cell.setCellValue(list.get(i).getGname());
            cell.setCellStyle(titleStyle2);
            //供应商类型
            cell = row.createCell(++count);
            if (list.get(i).getLeixing() == 0) {
                cell.setCellValue("自产");
            } else {
                cell.setCellValue("进货");
            }
            cell.setCellStyle(titleStyle2);
            //负责人
            cell = row.createCell(++count);
            cell.setCellValue(list.get(i).getPerson());
            cell.setCellStyle(titleStyle2);
            //联系方式
            cell = row.createCell(++count);
            cell.setCellValue(list.get(i).getPhone());
            cell.setCellStyle(titleStyle2);
            //地址
            cell = row.createCell(++count);
            cell.setCellValue(list.get(i).getAddress());
            cell.setCellStyle(titleStyle2);
        }
        for (int i = 0; i < count; i++) {
            sheet.setColumnWidth(i, 4000);
        }
        return wb;
    }

    @Override
    public int saveGongyingshang(Gongyingshang gongyingshang) {
        //修改
        if (gongyingshang.getId() != null && !"".equals(gongyingshang.getId())) {
            int code = gongyingshangMapper.findGongyingsByName(gongyingshang);
            if (code > 0) {
                throw new MyException("修改失败，已存在相同的供应商");
            }
            int codes = gongyingshangMapper.updateByPrimaryKeySelective(gongyingshang);
            if (codes <= 0) {
                throw new MyException("修改失败，请确认信息重新尝试");
            }
            return codes;
        } else {
            //新增
            //查询是否存在该供应商
            int count = gongyingshangMapper.findGongyingsByName(gongyingshang);
            if (count > 0) {
                throw new MyException("新增失败，已存在相同的供应商");
            }
            gongyingshang.setId(UUIDUtil.getUUID());
            int code = gongyingshangMapper.insertSelective(gongyingshang);
            if (code <= 0) {
                throw new MyException("新增失败，请检查数据重复");
            }
            return code;
        }
    }

    @Override
    public int findGname(Gongyingshang gongyingshang) {
        int code = gongyingshangMapper.findGongyingsByName(gongyingshang);
        if (code > 0) {
            throw new MyException("该名称已存在");
        }
        return code;
    }

    @Override
    public Gongyingshang findGongyingshangById(Gongyingshang gongyingshang) {
        Gongyingshang gongyingshangs = gongyingshangMapper.findGongyingshangById(gongyingshang);
        if (gongyingshangs == null) {
            throw new MyException("修改数据不存在，请确认信息重试");
        }
        return gongyingshangs;
    }

    @Override
    public int delGongyingshang(Gongyingshang gongyingshang) {
        int code = gongyingshangMapper.updateDeLFlag(gongyingshang);
        if (code <= 0) {
            throw new MyException("删除失败，请确认信息后重新尝试");
        }
        return code;
    }

    @Override
    public List<Gongyingshang> findCombobox(Gongyingshang gongyingshang) {
        return gongyingshangMapper.findCombobox(gongyingshang);
    }


}
